<?php
/**
 * 试戴统计表
 *
 * Author: Zeon
 * Date: 2018/4/19
 * Created by Panxsoft.
 */
namespace App\Models\Panx;

use App\Models\Customer\DeviceAccount;
use App\Models\OpenWechat\WechatUser;
use App\Models\Store\Store;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * @property
 * @mixin Builder
 */
class Record extends Model
{
    const TRY_ON = 1; // 记录设备账号商品试戴

    protected $table = 'records';

    protected $guarded = [];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function recorderable()
    {
        return $this->morphTo('recorderable', 'recorderable_type', 'recorderable_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function objectable()
    {
        return $this->morphTo('objectable', 'objectable_type', 'objectable_id', 'id');
    }

    /**
     * @param $device_account_id
     * @return int
     */
    public static function getProductTryOnCountByDeviceAccountId($device_account_id)
    {
        $product_try_on_count = (new self)->where('recorderable_id', $device_account_id)
            ->where([
                'recorderable_type' => class_basename(DeviceAccount::class),
                'record_type_id'    => RecordType::PRODUCT_TRYON
            ])
            ->count();

        return $product_try_on_count;
    }
}
